package com.lmb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmb.bean.Dish;
import com.lmb.bean.DishFlavor;
import com.lmb.dao.DishDao;
import com.lmb.dao.DishFlavorDao;
import com.lmb.dto.DishDto;
import com.lmb.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @projectName: reggie
 * @package: com.lmb.service.impl
 * @className: DishServicelmpl
 * @author: Eric
 * @description: TODO
 * @date: 2022/9/18 16:55
 */
@Service
public class DishServicelmpl extends ServiceImpl<DishDao, Dish> implements DishService {


    @Autowired
    private DishFlavorDao dishFlavorDao;


    @Override
    public boolean addDishAndDishFlavor(DishDto dishDto, HttpSession session) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(dishDto.getName() != null,Dish::getName,dishDto.getName());
        int count = count(dishLambdaQueryWrapper);
        if (dishDto.getName() == null ||count>0){
            throw new ClassCastException("菜品名重复");
        }

        Long employeeId = (Long) session.getAttribute("employee");
        dishDto.setCreateUser(employeeId) ;
        dishDto.setCreateTime(LocalDateTime.now());

        dishDto.setUpdateTime(LocalDateTime.now());
        dishDto.setUpdateUser(employeeId);

        boolean save = save(dishDto);

        dishDto.getFlavors().stream().forEach(dishFlavor -> {
            dishFlavor.setDishId(dishDto.getId());
            dishFlavor.setCreateUser(employeeId);
            dishFlavor.setCreateTime(LocalDateTime.now());
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavor.setUpdateUser(employeeId);
            dishFlavorDao.insert(dishFlavor);
        });
        return save?true:false;
    }

    @Override
    public void upStatuss(Long[] ids, Integer status, HttpSession session) {
            for (Long id : ids) {
                LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
                dishLambdaQueryWrapper.eq(id!= null,Dish::getId,id);
                Dish dish = new Dish();

                if (status==0){
                    dish.setStatus(0);
                }

                if (status==1){
                    dish.setStatus(1);
                }

                dish.setUpdateUser((Long) session.getAttribute("employee"));
                Long employee = (Long) session.getAttribute("employee");
                System.out.println("修改人"+employee);
                System.out.println(dish.getUpdateUser());
                dish.setUpdateTime(LocalDateTime.now());
                System.out.println(dish);
                update(dish, dishLambdaQueryWrapper);
            }

    }

    @Override
    public void deleteDishs(Long[] ids) {
        for (Long id : ids) {
            Dish dish = getById(id);
            if (!dish.getStatus().equals(0)){
                throw new ClassCastException("此菜品还在起售状态");
            }
            this.removeById(id);
        }
    }

    @Override
    public DishDto DishById(Long id) {
        System.out.println(id);
        DishDto dishDto = new DishDto();
        Dish dish = getById(id);
        BeanUtils.copyProperties(dish,dishDto);
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> dishFlavors = dishFlavorDao.selectList(dishFlavorLambdaQueryWrapper);
        dishDto.setFlavors(dishFlavors);
        System.out.println("                   "+dishDto);
        return dishDto;
    }

    @Override
    public void upDish(DishDto dishDto,HttpSession session) {

        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(dishDto.getName()!=null,Dish::getName,dishDto.getName());
        dishLambdaQueryWrapper.ne(Dish::getId,dishDto.getId());
        int count = count(dishLambdaQueryWrapper);
        if (count>0) {
            throw new ClassCastException("菜品名已重复");
        }
        dishDto.setUpdateUser((Long) session.getAttribute("employee"));
        dishDto.setUpdateTime(LocalDateTime.now());
        updateById(dishDto);

        dishDto.getFlavors().stream().forEach(dishFlavor -> {
            dishFlavor.setUpdateUser((Long) session.getAttribute("employee"));
            dishFlavor.setUpdateTime(LocalDateTime.now());
            dishFlavorDao.updateById(dishFlavor);
        });


    }

    @Override
    public List<DishDto> dishAndDishFlavorList(Long categoryId) {

        System.out.println(categoryId);
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(categoryId!= null,Dish::getCategoryId,categoryId);
        List<Dish> list = list(dishLambdaQueryWrapper);

        List<DishDto> dishDtos = new ArrayList<>();

        list.stream().forEach(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
            dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId,dish.getId());
            List<DishFlavor> dishFlavors = dishFlavorDao.selectList(dishFlavorLambdaQueryWrapper);
            dishDto.setFlavors(dishFlavors);
            dishDtos.add(dishDto);
        });
        return dishDtos;
    }
}
